home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTER6 / LODMENUI.C < prev    next >
C/C++ Source or Header  |  1996-01-27  |  8KB  |  239 lines

  1.  
  2. #include <windows.h>  
  3. #include "LodMenuI.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109.    switch( uMsg )
  110.    {
  111.        case WM_CREATE :
  112.                {
  113.                   LPTSTR  lpMem;
  114.                   WORD    wValue;
  115.                   HMENU   hMenu;
  116.  
  117.                   lpMem = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
  118.                                       sizeof( MENUITEMTEMPLATEHEADER ) +
  119.                                       (6*sizeof( MENUITEMTEMPLATE )) +
  120.                                       MAXMENUNAMES );
  121.  
  122.  
  123.                   // Set values for MENUITEMTEMPLATEHEADER structure.
  124.                   //.................................................
  125.                   wValue = 0;
  126.                   AppendMemory( lpMem, (LPTSTR)&wValue, sizeof( WORD ), 
  127.                                 TRUE, FALSE );
  128.                   wValue = 0;
  129.                   AppendMemory( lpMem, (LPTSTR)&wValue, sizeof( WORD ), 
  130.                                 FALSE, FALSE );
  131.  
  132.                   // Add Popup menu and items.
  133.                   //..........................
  134.                   wValue = MF_POPUP;
  135.                   AppendMemory( lpMem, (LPTSTR)&wValue, sizeof( WORD ), 
  136.                                 FALSE, FALSE );
  137.                   AppendMemory( lpMem, "&Popup", 7, FALSE, TRUE );
  138.  
  139.                   wValue = 0;
  140.                   AppendMemory( lpMem, (LPTSTR)&wValue, sizeof( WORD ),
  141.                                         FALSE, FALSE );
  142.                   wValue = IDM_FIRST;
  143.                   AppendMemory( lpMem, (LPTSTR)&wValue, sizeof( WORD ), 
  144.                                         FALSE, FALSE );
  145.                   AppendMemory( lpMem, "&First", 7, FALSE, TRUE );
  146.  
  147.                   wValue = MF_END;  // Last Item in Popup menu.
  148.                   AppendMemory( lpMem, (LPTSTR)&wValue, sizeof( WORD ), 
  149.                                         FALSE, FALSE );
  150.                   wValue = IDM_SECOND;
  151.                   AppendMemory( lpMem, (LPTSTR)&wValue, sizeof( WORD ), 
  152.                                         FALSE, FALSE );
  153.                   AppendMemory( lpMem, "&Second", 8, FALSE, TRUE );
  154.  
  155.                   // Add last item on menu bar, Exit.
  156.                   //.................................
  157.                   wValue = MF_END;
  158.                   AppendMemory( lpMem, (LPTSTR)&wValue, sizeof( WORD ), 
  159.                                         FALSE, FALSE );
  160.                   wValue = IDM_EXIT;
  161.                   AppendMemory( lpMem, (LPTSTR)&wValue, sizeof( WORD ), 
  162.                                         FALSE, FALSE );
  163.                   AppendMemory( lpMem, "E&xit", 6, FALSE, TRUE );
  164.  
  165.                   // Create the menu and set it as the applicationÆs menu.
  166.                   //......................................................
  167.                   hMenu = LoadMenuIndirect( lpMem );    
  168.                   SetMenu( hWnd, hMenu );
  169.  
  170.                   HeapFree( GetProcessHeap(), 0, lpMem );
  171.                }
  172.                break;
  173.  
  174.       case WM_COMMAND :
  175.               switch( LOWORD( wParam ) )
  176.               {
  177.                  case IDM_FIRST :
  178.                         MessageBox( hWnd, "First Menu Item Worked.", 
  179.                                     "Message", MB_OK | MB_ICONINFORMATION );
  180.                         break;
  181.  
  182.                  case IDM_SECOND :
  183.                         MessageBox( hWnd, "Second Menu Item Worked.", 
  184.                                     "Message", MB_OK | MB_ICONINFORMATION );
  185.                         break;
  186.  
  187.                  case IDM_EXIT :
  188.                         DestroyWindow( hWnd );
  189.                         break;
  190.               }
  191.               break;
  192.       
  193.       case WM_DESTROY :
  194.               PostQuitMessage(0);
  195.               break;
  196.  
  197.       default :
  198.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  199.    }
  200.  
  201.    return( 0L );
  202. }
  203.  
  204.  
  205. VOID AppendMemory( LPTSTR lpDest, LPTSTR lpSource, int nBytes, BOOL bReset, 
  206.                    BOOL bConvert )
  207. {
  208. static int   nLastEnd;
  209. static WCHAR wBuf[30];
  210.  
  211.    LPTSTR lps, lpd;
  212.    
  213.    lps = lpSource;
  214.    lpd = lpDest;
  215.  
  216.    if ( bReset )
  217.       nLastEnd = 0;
  218.    else
  219.       lpd += nLastEnd;
  220.  
  221.    // If this is a character string, we need to convert it to Unicode(TM).
  222.    // This is true for both Windows NT and Windows 95.
  223.    //.....................................................................
  224.    if ( bConvert )
  225.    {
  226.       MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, lpSource, -1, &wBuf[0], 30 );
  227.        
  228.       memcpy( lpd, &wBuf[0], nBytes*sizeof( WCHAR ) );
  229.       nLastEnd += nBytes*sizeof( WCHAR );
  230.    }
  231.    else
  232.    {
  233.       memcpy( lpd, lps, nBytes );
  234.       nLastEnd += nBytes;
  235.    }
  236. }
  237.  
  238.  
  239.